package org.emet.dao;

import static org.emet.Constants.STATUS_OK;
import static org.emet.Constants.STATUS_ORDERED;

import java.util.List;

import org.emet.entity.biz.Vehicle;
import org.emet.jee.core.dao.Page;
import org.emet.jee.core.dao.arch.ArchDataSourceBaseDao;
import org.emet.service.dto.VehicleCriteria;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

@Repository
public class VehicleDao extends ArchDataSourceBaseDao<Vehicle, Long> {
	private static final String entityName = Vehicle.class.getSimpleName();
	String Q_VLIAD_BY_TITILE = "from " + entityName + " where  status = ? or status = ?";

	public List<Vehicle> getValidVehicles() {
		List<Vehicle> list = find(Q_VLIAD_BY_TITILE, STATUS_OK, STATUS_ORDERED);
		return list;
	}

	public Page<Vehicle> search(Page<Vehicle> page, VehicleCriteria criteria) {
		Criteria c = getSession().createCriteria(Vehicle.class);

		NonEmptyValueHashMap<String, Object> propertyNameValues = new NonEmptyValueHashMap<String, Object>();
		propertyNameValues.put("lpn", criteria.getLpn());
		propertyNameValues.put("status", criteria.getStatus());
		propertyNameValues.put("type", criteria.getType());

		c.add(Restrictions.allEq(propertyNameValues));
		page.setOrderBy("id");
		page.setOrder("desc");
		return findPage(page, c);
	}

	public Vehicle getByLpn(String lpn) {
		return findUniqueBy("lpn", lpn);
	}

}
